package com.qiangesoft.blog.controller.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.qiangesoft.blog.common.result.ResponseResult;
import com.qiangesoft.blog.system.entity.SysUser;
import com.qiangesoft.blog.system.pojo.dto.*;
import com.qiangesoft.blog.system.service.ISysUserService;
import com.qiangesoft.blog.system.utils.AesEncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author qiange
 */
@RequestMapping("/api/user")
@RestController
@Api(tags = "登录相关")
@RequiredArgsConstructor
public class ApiLoginController {

    private final ISysUserService sysUserService;

    @RequestMapping(value = "/emailLogin", method = RequestMethod.POST)
    @ApiOperation(value = "邮箱登录", httpMethod = "POST", response = ResponseResult.class, notes = "邮箱登录")
    public ResponseResult emailLogin(@Valid @RequestBody EmailLoginDTO emailLoginDTO) {
        return sysUserService.emailLogin(emailLoginDTO);
    }

    @RequestMapping(value = "/emailRegister", method = RequestMethod.POST)
    @ApiOperation(value = "邮箱账号注册", httpMethod = "POST", response = ResponseResult.class, notes = "邮箱账号注册")
    public void emailRegister(@Valid @RequestBody EmailRegisterDTO emailRegisterDTO) {
        sysUserService.emailRegister(emailRegisterDTO);
    }

    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public ResponseResult updatePassword(@Valid @RequestBody EmailRegisterDTO emailRegisterDTO) {
        return sysUserService.updatePassword(emailRegisterDTO);
    }

    @RequestMapping(value = "/qqLogin", method = RequestMethod.POST)
    @ApiOperation(value = "QQ登录", httpMethod = "POST", response = ResponseResult.class, notes = "QQ登录")
    public ResponseResult login(@Valid @RequestBody QQLoginDTO qqLoginDTO) {
        return sysUserService.qqLogin(qqLoginDTO);
    }

    @RequestMapping(value = "/gitEELogin", method = RequestMethod.GET)
    @ApiOperation(value = "gitEE登录", httpMethod = "GET", response = ResponseResult.class, notes = "gitEE登录")
    public ResponseResult gitEELogin(String code) {
        return sysUserService.giteeLogin(code);
    }

    @RequestMapping(value = "/weiboLogin", method = RequestMethod.GET)
    @ApiOperation(value = "微博登录", httpMethod = "GET", response = ResponseResult.class, notes = "微博登录")
    public ResponseResult weiboLogin(String code) {
        return sysUserService.weiboLogin(code);
    }

    @RequestMapping(value = "/sendEmailCode", method = RequestMethod.GET)
    @ApiOperation(value = "发送邮箱验证码", httpMethod = "GET", response = ResponseResult.class, notes = "发送邮箱验证码")
    public ResponseResult sendEmailCode(String email) {
        return sysUserService.sendEmailCode(email);
    }

    @RequestMapping(value = "/bindEmail", method = RequestMethod.POST)
    @SaCheckLogin
    public ResponseResult bindEmail(@RequestBody UserAuthDTO vo) {
        return sysUserService.bindEmail(vo);
    }

    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@Validated @RequestBody LoginDTO loginDTO) {
        SysUser sysUser = sysUserService.getByUsername(loginDTO.getUsername());
        if (sysUser == null) {
            ResponseResult.error("账号不存在");
        }
        String encrypt = AesEncryptUtils.aesEncrypt(loginDTO.getPassword());
        if (!encrypt.equals(sysUser.getPassword())) {
            ResponseResult.error("账号或密码错误");
        }
        StpUtil.login(sysUser.getId(), "pc");
        return ResponseResult.success(StpUtil.getTokenValue());
    }

}

